package com.kerrykidz.promotion.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.dao.model.GblPosDiscountEntity;
import com.kerrykidz.promotion.dao.mapper.ProCampaignEntityMapper;
import com.kerrykidz.promotion.dao.model.ProCampaignEntity;
import com.kerrykidz.promotion.dao.model.ProGblPrdEntity;
import com.kerrykidz.promotion.service.IPromotionService;
import com.kerrykidz.scminvoicing.dao.mapper.GblPosDiscountEntityMapper;
import com.kerrykidz.system.util.ConstantUtil;
import com.kerrykidz.system.util.DataSourceUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.service.IWorkflowService;
import com.kerrykidz.workflow.vo.WorkflowParamVo;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.lang.StringUtil;
import com.rdp.util.model.TreeModel;
/**
 * 
 * @ClassName: PromotionServiceImpl 
 * @description: 大致描述该类的用途，内容简短易懂 
 * @author: wolf-yansl 
 * @date: 2014年9月9日 下午2:12:51 
 * @version: V1.0
 *
 */
@Service
public class PromotionServiceImpl implements IPromotionService {
	
	public static final String  workFlow_key = "discount";
	
	@Autowired
	private ProCampaignEntityMapper proCampaignEntityMapper;
	@Autowired
	private GblPosDiscountEntityMapper GblPosDiscountEntityMapper;
	@Autowired
	private IWorkflowService workflowService;
	@Autowired
	private DataSourceUtil das;
	
	@Override
	public List<ProCampaignEntity> findLikeCampNameOrTime(ProCampaignEntity entity,HttpSession session) throws RDPException {
		List<ProCampaignEntity> resultList = new ArrayList<ProCampaignEntity>();
		entity.setIspaging(true);
		
		resultList = proCampaignEntityMapper.getLikeCampNameOrTime(entity);
		for(ProCampaignEntity p :resultList){
			p.setWorkflowVo(workflowService.getWorkflowVo(p.getFkFlowinstId(), p.getStatus(), UserUtil.getUserFromSession()));
		}
		return resultList;
	}

	@Override
	public ProCampaignEntity saveProCampaignEntity(ProCampaignEntity entity,List<String> prdlist,UserInfo user)throws RDPException {
		entity.setPrdlist(prdlist);
		entity.setModiTime(new Date());
		/**
		 * 保存 只保存并且状态为草稿  1
		 * 提交 只保存并且启动   流程实例   状态为审批中 2
		 **/
		String status = entity.getStatus();
		if(!status.equals("1")){
			entity.setStatus("2");
		}
		
		
		/**启动流程实例*/
		if(status.equals("2")){
			/**调用工作流程接口  返回流程实例ID */
			
			String fkFlowId = workflowService.startWorkflow(PromotionServiceImpl.workFlow_key, entity.getPkCampId(), user);
			entity.setFkFlowinstId(fkFlowId);
			
		}
		proCampaignEntityMapper.saveEntity(entity);
		/**分配促销商品*/
		if(prdlist != null && prdlist.size() > 0){
			Map<String,Object> prdMap = new HashMap<String,Object>();
			prdMap.put("pkCampId", entity.getPkCampId());
			prdMap.put("prdList", prdlist);
			proCampaignEntityMapper.addProCampPrd(prdMap);
		}
		
		//判断是否是门店
		if(StringUtil.isNotBlank(user.getBranId()) && status.equals("2")){
			//如果是门店的话就进行数据上传
			if(user.getBranId().indexOf("ZB") == -1){
				das.executeDiscount(entity, "insert");
			}
		}
		
		return entity;
	}

	@Override
	public ProCampaignEntity updateProCampaignEntity(ProCampaignEntity entity, List<String> prdlist,UserInfo user) throws RDPException {
		ProCampaignEntity paraEntity = new ProCampaignEntity();
		paraEntity.setPkCampId(entity.getPkCampId());
		
		/**参数拷贝*/
		ProCampaignEntity dbEntity = findByCampId(paraEntity);
		if(dbEntity != null){
		
			/**提交方式*/
			String status = entity.getStatus();
			if(!status.equals("1")){
				entity.setStatus("2");
			}
			
			/**启动流程实例*/
			if(status.equals("2")){
				/**调用工作流程接口  返回流程实例ID */
				String fkFlowId = workflowService.startWorkflow(PromotionServiceImpl.workFlow_key, entity.getPkCampId(), user);
				entity.setFkFlowinstId(fkFlowId);
			}
			BeanUtils.copyProperties(entity, dbEntity);
			dbEntity.setModiTime(new Date());
			proCampaignEntityMapper.updateProCampaignEntity(dbEntity);
			
			/**删除之前商品*/
			proCampaignEntityMapper.deleteProCampidPrdAll(entity.getPkCampId());
			/**分配促销商品*/
			if(prdlist != null && prdlist.size() > 0){
				Map<String,Object> prdMap = new HashMap<String,Object>();
				prdMap.put("pkCampId", entity.getPkCampId());
				prdMap.put("prdList", prdlist);
				proCampaignEntityMapper.addProCampPrd(prdMap);
			}
			
			//判断是否是门店
			if(StringUtil.isNotBlank(user.getBranId()) && status.equals("2")){
				//如果是门店的话就进行数据上传
				if(user.getBranId().indexOf("ZB") == -1){
					das.executeDiscount(dbEntity, "insert");
				}
			}
		}
		return null;
	}
	
	@Override
	public ProCampaignEntity updateProCampaignFlow(ProCampaignEntity entity,List<String> prdlist, UserInfo user) throws RDPException {
		ProCampaignEntity paraEntity = new ProCampaignEntity();
		paraEntity.setPkCampId(entity.getPkCampId());
		
		/**参数拷贝*/
		ProCampaignEntity dbEntity = findByCampId(paraEntity);
		if(dbEntity != null){
			
			/**
				workflowparam.getProcessInstanceId()//实例ID
				workflowparam.getUserInfo()//登录用户
			 * */
			WorkflowParamVo flow = new WorkflowParamVo();
			flow.setProcessInstanceId(dbEntity.getFkFlowinstId());
			flow.setHandleAdviceDes(ConstantUtil.WORKFLOW_STATUS_REAPPLY);
			flow.setUserInfo(user);
			/**调用工作流程接口  返回流程实例ID */
			workflowService.complete(flow);
			
			BeanUtils.copyProperties(entity, dbEntity);
			dbEntity.setModiTime(new Date());
			proCampaignEntityMapper.updateProCampaignEntity(dbEntity);
			
			/**删除之前商品*/
			proCampaignEntityMapper.deleteProCampidPrdAll(entity.getPkCampId());
			/**分配促销商品*/
			if(prdlist != null && prdlist.size() > 0){
				Map<String,Object> prdMap = new HashMap<String,Object>();
				prdMap.put("pkCampId", entity.getPkCampId());
				prdMap.put("prdList", prdlist);
				proCampaignEntityMapper.addProCampPrd(prdMap);
			}
			
			/*if(StringUtils.isNotBlank(entity.getIsRel())){*/
				//判断是否是门店
				if(StringUtil.isNotBlank(user.getBranId())){
					//如果是门店的话就进行数据上传
					if(user.getBranId().indexOf("ZB") == -1){
						das.executeDiscount(dbEntity, "update");
					}
				}
			/*}*/
		}
		return null;
	}
	
	@Override
	public void updateProCampaignStatus(ProCampaignEntity entity)throws RDPException {
		entity.setModiTime(new Date());
		proCampaignEntityMapper.updateProCampaignStatus(entity);
	}
	@Override
	public void deleteProCampaignEntity(ProCampaignEntity entity,UserInfo user)throws RDPException {
		ProCampaignEntity paraEntity = new ProCampaignEntity();
		paraEntity.setPkCampId(entity.getPkCampId());
		
		/**参数拷贝*/
		ProCampaignEntity dbEntity = findByCampId(paraEntity);
		if(dbEntity != null){
			dbEntity.setDelFlag("1");
			dbEntity.setModiTime(new Date());
			proCampaignEntityMapper.deleteProCampaignEntity(dbEntity);
			
			if(StringUtils.isNotBlank(paraEntity.getFkFlowinstId())){
				WorkflowParamVo workFlow = new WorkflowParamVo();
				workFlow.setProcessInstanceId(paraEntity.getFkFlowinstId());
				workFlow.setUserInfo(user);
				workflowService.deleteProcessInstance(workFlow);
			}
		}
	}
	@Override
	public List<GblPosDiscountEntity> findGblDiscountAll() throws RDPException {
		return GblPosDiscountEntityMapper.findAllDiscount();
	}

	@Override
	public List<ProGblPrdEntity> findLikePrdIdOrPrdName(ProGblPrdEntity entity)throws RDPException {
		entity.setIspaging(true);
		return proCampaignEntityMapper.getLikePrdIdOrPrdName(entity);
	}
	
	@Override
	public List<ProGblPrdEntity> findByCatePrdIdOrPrdName(ProGblPrdEntity entity, String pid) throws RDPException {
		List<TreeModel> dataList = new ArrayList<TreeModel>();
		TreeModel treeModel = new TreeModel();
		treeModel.setId(pid);
		dataList.add(treeModel);
		findPrdCateAll(pid, dataList);
		
		entity.setIspaging(true);
		entity.setCateIdTreeModelList(dataList);
		return proCampaignEntityMapper.getLikePrdIdOrPrdName(entity);
	}

	@Override
	public void findPrdCateAll(String pid,List<TreeModel> noteList) throws RDPException {
		List<TreeModel> superNodeList = proCampaignEntityMapper.getPrdCateChild(pid);
		if(superNodeList != null && superNodeList.size() > 0){
			for(TreeModel tree : superNodeList){
				tree.setOpen(true);
				noteList.add(tree);
				if(tree.getNodeCount() > 0){
					findPrdCateAll(tree.getId(), noteList);
				}
			}
		}
	}

	@Override
	public ProCampaignEntity findByCampId(ProCampaignEntity entity) throws RDPException {
		/*List<ProCampaignEntity> resultList = new ArrayList<ProCampaignEntity>();
		resultList = proCampaignEntityMapper.getLikeCampNameOrTime(entity);*/
		entity = proCampaignEntityMapper.getByCampId(entity);
		/*if(resultList != null && resultList.size() == 1){
			return resultList.get(0);
		}*/
		return entity;
	}
	
	public List<String> findByProCampPrdIdAll(String campId) throws RDPException {
		List<String> resultList = new ArrayList<String>();
		resultList = proCampaignEntityMapper.getByProCampidPrdAll(campId);
		return resultList;
	}

	



	
}
